<template>
  <base-breadcrumb>
    <searchData :searchData.sync="searchArr" @search="search"></searchData>
    <a-card>
      <div class="table-operator">
        <a-button v-btnPermission="'hse_010_add_01'" type="primary" icon="plus" @click="handleUpdate('add', {})" v-if="hideBuquBtn()">新增会议 </a-button>
      </div>
      <list-table ref="table" size="default" rowKey="key" :columns="columns" :request="loadData" :alert="true" show-size-changer show-quick-jumper showPagination="auto">
        <template slot="sort" slot-scope="text, record, index">{{ index + 1 }}</template>
        <template slot="startTime" slot-scope="text, record">{{ $dateFormat(text, 'D') }}</template>
        <template slot="commentedPlan" slot-scope="text, record">{{ record.meetingExtraVO.commentedPlan }}</template>
        <template slot="professionComName" slot-scope="text, record">{{ record.meetingExtraVO.professionComName }} </template>
        <template slot="avgScore" slot-scope="text, record">{{ record.meetingExtraVO.avgScore }}</template>
        <template slot="status" slot-scope="text, record">
          <list-status :status="record.status" conversion="en" :text="text"></list-status>
        </template>
        <div slot="operation" slot-scope="text, record">
          <MoreButton>
          <a @click="look(record)" v-btnPermission="'hse_010_loo_03'">查看会议</a>
          <a @click="edit(record)" v-if="record.status === 20 && $isMe(record.creatorId)&&hideBuquBtn()" v-btnPermission="'hse_010_edi_02'">编辑</a>
          <a v-btnPermission="'hse_010_del_03'" @click="handleUpdate('del', record)" v-if="hideBuquBtn()">删除</a>
          </MoreButton>
        </div>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { STable } from '@/components'
import listStatus from '@/components/list_status/meeting_status'
import { deleteById, getMeetingList } from '@/api/office/conference'
import meetingTypeApi from '@/api/safetySupervision/base/meetingType.js'
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    listStatus
  },
  data() {
    return {
      queryParam: {},
      columns: [
        {
          title: '序号',
          dataIndex: 'sort',
          width:80, 
          scopedSlots: { customRender: 'sort' }
        },
        {
          title: '会议名称',
          dataIndex: 'meetingName',
          width:200, 
        },
        {
          title: '会议类型',
          dataIndex: 'meetingKeyName',
          width:200, 
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName',
          width: 150
        },
        {
          title: '所属项目',
          dataIndex: 'projectName',
          width:200, 
        },
        {
          title: '召开时间',
          dataIndex: 'startTime',
          width:120, 
          scopedSlots: { customRender: 'startTime' }
        },

        {
          title: '参会人数',
          dataIndex: 'meetingCount',
          width: 100
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 120,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 180,
          scopedSlots: { customRender: 'operation' }
        }
      ],
      searchArr: [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '所属项目',
        //   keyValue: 'branchComId',
        //   valueKey: 'projectId',
        //   type: SelectProject
        // },
        {
          name: '会议名称',
          value: 'meetingName',
          type: 'input'
        },
        {
          name: '召开时间',
          value: 'startEndDate',
          type: 'dateRange'
        },
        {
          name: '状态',
          value: 'status',
          type: 'select',
          data: [
            {
              value: '20',
              label: '待开始'
            },
            {
              value: '30',
              label: '进行中'
            },
            // {
            //   value: '35',
            //   label: '审批中'
            // },
            {
              value: '40',
              label: '已完结'
            }
          ]
        },
        {
          name: '会议类型',
          value: 'meetingKeyIds',
          type: 'select',
          data: [],
          attrs: {
            mode: 'multiple'
          }
        }
      ],
      loadData: async (parameter) => {
        this.queryParam.query = { ...this.queryParam.query, typeId: 5 }
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        const [err, res] = await getMeetingList(requestParameters)
        return new Promise((resolve, reject) => {
          if (err) {
            reject(err)
          } else {
            resolve(res)
          }
        })
      }
    }
  },
  mounted() {
    this.meetingTypeListReq()
  },
  methods: {
    async meetingTypeListReq() {
      let res = await meetingTypeApi.list({}, { pageNo: 1, pageSize: 1000, query: { type: 13 } })
      if (res.code === this.SUCCESS_CODE) {
        res.data.forEach((m) => {
          m.label = m.name
          m.value = m.id
        })
        console.log(res.data)

        this.searchArr[this.searchArr.length - 1].data = res.data
        console.log(this.searchArr)
      }
    },
    look(record) {
      let pageType
      if (record.status === 20) pageType = 'start'
      if (record.status === 30) pageType = 'edit'
      if (record.status === 35 || record.status === 40) pageType = 'look'
      this.$router.push({
        name: 'safetyEducationInputSummary',
        query: { id: record.id || '', pageType }
      })
    },
    edit(record) {
      this.$router.push({
        name: 'safetyEducationAddConference',
        query: {
          id: record.id || '',
          pageType: record ? 'edit' : 'add',
          typeId: 5,
          printType: 'education'
        }
      })
    },
    //搜索
    search(val = {}) {
      this.queryParam.query = {
        ...val,
        typeId: 5,
        hsemonth: (val.month && val.month.format('YYYY-MM')) || void 0,
        startTime: (val.startEndDate && val.startEndDate.length && val.startEndDate[0].format('YYYY-MM-DD')) || void 0,
        endTime: (val.startEndDate && val.startEndDate.length && val.startEndDate[1].format('YYYY-MM-DD')) || void 0
      }
      console.log(this.queryParam)

      this.$refs.table.refresh(true)
    },

    // 处理更新操作（新增、编辑）
    async handleUpdate(type, data = {}) {
      if (type == 'del') {
        this.$submitConfirm(async () => {
          const [err, res] = await deleteById({ id: data.id })
          if (!err) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(true)
          }
        }, '确认删除？')
      } else if (type == 'look' || type == 'start') {
        this.$router.push({
          name: 'safetyEducationInputSummary',
          query: {
            id: data.id || '',
            pageType: type,
            printType: 'education'
          }
        })
      } else {
        this.$router.push({
          name: 'safetyEducationAddConference',
          query: {
            id: data.id || '',
            pageType: type,
            typeId: 5
          }
        })
      }
    }
  }
}
</script>
